package com.zx.cloud.controller;

import com.google.common.collect.ImmutableMap;
import com.zx.cloud.RestStatus;

/**
 * @author Alex
 */

public enum StatusCode implements RestStatus {

    OK(20000, "请求成功"),

    // 40xxx 客户端不合法的请求
    INVALID_MODEL_FIELDS(40001, "字段校验非法"),

    /**
     * 参数类型非法，常见于SpringMVC中String无法找到对应的enum而抛出的异常
     */
    INVALID_PARAMS_CONVERSION(40002, "参数类型非法"),
    /**
     * 用于注册时用户已经存在的情况
     */
    USER_EXISTS(42003, "用户已经存在, 请尝试登录"),

    /**
     * 用于登录时用户不存在的情况
     */
    USER_NOT_EXISTS(42004, "用户不存在, 请先注册"),

    /**
     * 凭证错误
     */
    INVALID_CREDENTIAL(42005, "用户名或密码错误"),
    // 41xxx 请求方式出错
    
   
    /**
     * http media type not supported
     */
    HTTP_MESSAGE_NOT_READABLE(41006, "HTTP消息不可读"),

    /**
     * 请求方式非法
     */
    REQUEST_METHOD_NOT_SUPPORTED(41007, "不支持的HTTP请求方法"),

    // 成功接收请求, 但是处理失败
    /**
     * Duplicate Key
     */
    DUPLICATE_KEY(42008, "操作过快, 请稍后再试"),
    
    /**
     * 无操作权限
     */
    INVALID_ACCESS(42009, "无操作权限"),
    
    
    TOKEN_EXPIRED(42010, "X-Auth-Token过期,刷新后重新访问!"),
    
    
    FILE_UPLOAD_ERROR(42011, "文件上传失败!"),
    
    OLD_PASSWORD_ERROR(42012, "原密码输入错误"),
    
    SMS_CODE_ERROR(42013, "验证码输入错误"),
    
    NVALID_ORDER_LOAN(43001, "还有正在处理的借款订单！"),
    
    INVALID_ORDER_REPAY(43002, "还有未结清的借款订单！"),
    
    INVALID_ORDER(43000, "你尚未符合申请条件"),
    
    // 50xxx 服务端异常
    /**
     * 用于处理未知的服务端错误
     */
    SERVER_UNKNOWN_ERROR(50001, "服务端异常, 请稍后再试"),

    /**
     * 用于远程调用时的系统出错
     */
    SERVER_IS_BUSY_NOW(50002, "系统繁忙, 请稍后再试");

    private final int code;

    private final String message;

    private static final ImmutableMap<Integer, StatusCode> CACHE;

    static {
        final ImmutableMap.Builder<Integer, StatusCode> builder = ImmutableMap.builder();
        for (StatusCode statusCode : values()) {
            builder.put(statusCode.code(), statusCode);
        }
        CACHE = builder.build();
    }

    StatusCode(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public static StatusCode valueOfCode(int code) {
        final StatusCode status = CACHE.get(code);
        if (status == null) {
            throw new IllegalArgumentException("No matching constant for [" + code + "]");
        }
        return status;
    }

    @Override
    public int code() {
        return code;
    }

    @Override
    public String message() {
        return message;
    }

}
